﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POSSystem.DAL;

namespace POSSystem.BLL
{
    public class Brands : _Brands
    {
        public static Brands LoadAllActiveBrands()
        {
            try
            {
                Brands brands = new Brands();
                brands.Where.Deleted.Value = false;
                brands.Where.BranchID.Value = Branches.CurrentBranchID;
                brands.Query.Load();

                return brands;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("Brands.cs", "LoadAllActiveBrands", ex.StackTrace);
                throw new Exception("Brands.LoadAllActiveBrands(): " + ex.Message);
            }
        }

        public static Brands GetBrandByName(string name, int branchID)
        {
            try
            {
                Brands b = new Brands();
                b.Where.Deleted.Value = false;
                b.Where.BrandName.Value = name;
                b.Where.BranchID.Value = branchID;
                b.Query.Load();

                return b;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Categories.cs", "GetBrandByName", e.StackTrace);
                throw e;
            }
        }

        public static Brands GetBrandByStockKey(int branchID, int stockKey)
        {
            try
            {
                Brands b = new Brands();
                b.Where.Deleted.Value = false;
                b.Where.BranchID.Value = branchID;
                b.Where.StockKey.Value = stockKey;
                b.Query.Load();

                return b;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("Products.cs", "GetBrandByStockKey", e.StackTrace);
                throw e;
            }
        }

        public static Brands LoadAllActiveBrands(int branchID)
        {
            try
            {
                Brands brands = new Brands();
                brands.Where.Deleted.Value = false;
                brands.Where.BranchID.Value = branchID;
                brands.Query.Load();

                return brands;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("Brands.cs", "LoadAllActiveBrands", ex.StackTrace);
                throw new Exception("Brands.LoadAllActiveBrands(): " + ex.Message);
            }
        }

        public static bool IsBrandInUse(int brandID)
        {
            try
            {
                Products p = new Products();
                p.Where.Deleted.Value = false;
                p.Where.BrandID.Value = brandID;
                p.Where.BranchID.Value = Branches.CurrentBranchID;
                p.Query.Top = 1;
                p.Query.AddResultColumn(Products.ColumnNames.ID);
                p.Query.Load();

                return p.RowCount > 0;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("Brands.cs", "IsBrandInUse", ex.StackTrace);
                throw new Exception("Brands.IsBrandInUse(): " + ex.Message);
            }
        }
    }
}
